/*
 * Created on 2005-6-12
 * 
 * To change the template for this generated file go to Window - Preferences -
 * Java - Code Generation - Code and Comments
 */
package leeon;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import leeon.util.HtmlUtil;
import leeon.util.DateIterator;
import leeon.util.PropertyLoader;
import ipworks.*;

/**
 * @author Administrator
 * 
 * To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Generation - Code and Comments
 */
public class HttpDownloader {

	public static void main(String[] args) throws Exception {
		HttpDownloader hd = null;
		if (args.length != 0)
			hd = new HttpDownloader(args[0]);
		else
			hd = new HttpDownloader();
		hd.start();
	}
	
	
	
	private Http http = new Http();	
	ByteArrayOutputStream content = new ByteArrayOutputStream();
	
	
	private DateIterator di = null;
	private String site = null;
	private String path = null;
	private String href1 = null;
	private String href2 = null;
	private String delim = null;
	private String file = null;
	private String log = null;
	private int retry = 1;
	
	
	
	public HttpDownloader() throws Exception {
		this("downloader.conf");
	}
	
	public HttpDownloader(String confPath) throws Exception {						
		init(confPath);						
	}
	
	private void init(String confPath) throws Exception {				
		System.out.println("read conf file from ["+ confPath + "] ...");
		PropertyLoader pl = PropertyLoader.newInstance(confPath);
		
		site = pl.getProperty("page.site");
		path = pl.getProperty("page.path");
		href1 = pl.getProperty("page.href1");
		href2 = pl.getProperty("page.href2");
		delim = pl.getProperty("page.delim");
		
		if (pl.getProperty("page.retry") != null) {
			retry = Integer.parseInt(pl.getProperty("page.retry"));
		}
		
		file = pl.getProperty("file.path");
		log = pl.getProperty("file.log");								

		if ("true".equals(pl.getProperty("proxy.enable"))) {
			System.out.println("read proxy information from conf");
			http.setProxyServer(pl.getProperty("proxy.site"));
			http.setProxyPort(Integer.parseInt(pl.getProperty("proxy.port")));
			
			if (pl.getProperty("proxy.user") != null && pl.getProperty("proxy.user").length() != 0)
				http.setProxyUser(pl.getProperty("proxy.user"));
			
			if (pl.getProperty("proxy.password") != null && pl.getProperty("proxy.password").length() != 0)
				http.setProxyPassword(pl.getProperty("proxy.password"));
		}
		
		if ("true".equals(pl.getProperty("date.enable"))) {
			System.out.println("read date information from conf");
			if (pl.getProperty("date.pattern") != null && pl.getProperty("date.pattern").length() != 0)
				di = new DateIterator(pl.getProperty("date.start"), pl.getProperty("date.end"), pl.getProperty("date.pattern"));
			else if (pl.getProperty("date.end") != null && pl.getProperty("date.end").length() != 0)
				di = new DateIterator(pl.getProperty("date.start"), pl.getProperty("date.end"));
			else if(pl.getProperty("date.start") != null && pl.getProperty("date.start").length() != 0)
				di = new DateIterator(pl.getProperty("date.start"));
			else
				di = new DateIterator();			
		} else {
			di = new DateIterator();
		}
		
		http.addHttpEventListener(new HttpDownloaderEventListener(this));
		http.setFollowRedirects(1);
	}
	
	public void start() throws IOException {
		String u = null;
		String l = null;
		int i = 0;
		System.out.println("start download ...");
		while (di.hasNext()) {			
			
			u = site + di.get(path);
			System.out.println(u);
			this.content.reset();
			
			i = 0;
			while (i++ < this.retry) {
				try {
					http.get(u);
					i = this.retry;
				} catch (IPWorksException e) {
					e.printStackTrace();
				}
			}
			
			u = site + HtmlUtil.findHref(this.content.toString(), href1, delim);
			System.out.println(u);
			this.content.reset();			
			
			i = 0;
			while (i++ < this.retry) {
				try {
					http.get(u);
					i = this.retry;
				} catch (IPWorksException e) {
					e.printStackTrace();
				}
			}
			
			u = site + HtmlUtil.findHref(this.content.toString(), href2, delim);
			System.out.println(u);
			l = u + "\r";
			this.content.reset();			
			
			i = 0;
			while (i++ < this.retry) {
				try {
					http.get(u);
					i = this.retry;
				} catch (IPWorksException e) {
					e.printStackTrace();
				}
			}
			
			u = di.get(file);
			HtmlUtil.saveBytesToFile(this.content.toByteArray(), u, HtmlUtil.WHEN_EXIST_BACKUP);
			
			u = di.get(log);
			HtmlUtil.saveBytesToFile(l.getBytes(), u, HtmlUtil.WHEN_EXIST_APPEND);
			
			di.next();
		}
		System.out.println("finish download ...");
		this.content.close();
	}
	
}
